<?php
/*
 * 用户当日考勤记录，用户当日请假记录
 * @kenku
 */
apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class('Bll_Attendance_AccessDayRecord');
apf_require_class('Bll_Attendance_UserInfo');
apf_require_class('Bll_Attendance_UserKqStatus');
apf_require_class('Bll_Attendance_DailyLeave');
apf_require_class('Bll_Attendance_DailyReport');
apf_require_class('Const_LeaveType');

class Oa_Job_Attendance_UserDaystatus extends Oa_Job_Attendance_Abstract{

    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    /**时间点**/
    private $am_start		= 0900;
    private $am_later 		= 0910;
    private $am_absentee    = 0930;
    private $pm_absentee 	= 1400;
    private $pm_early 		= 1730;
    private $pm_off_work 	= 1800;

    //前一天
    private $pr_date = '';

    public function run(){
        $this->pr_date = date('Ymd',time()-24*60*60);
        $this->update_day_status($this->pr_date);
    }

    /**
     * job 每日 0 点运行，更新员工每日最终状态（是否上午迟到，上午缺勤，下午早退，下午缺勤）
     */
    public function update_day_status($last_day){

        $last_day = date('Ymd',time()-24*60*60);//上一天的时间点

        $user_day_record = $this->get_user_day_record();

        $bll_daily_leave_ = new Bll_Attendance_DailyLeave();
        $bll_daily_report = new Bll_Attendance_DailyReport();
        foreach ($user_day_record as $key => $value){

            $value['days'] = 0;
            if(empty($value['pm_type'])){
                //下午没有请假记录，继续查询是否有‘公出’记录
                $leave = $bll_daily_leave_->find_user_leave_daily_by_dateline($value['user_id'], $last_day);
                if (empty($leave)) {//没有公出
                    //即没有请假，也没有公出，且如果下午‘缺勤’，‘早退’则修改 user_day_status 中的‘下午默认正常’=>'下午考勤状态为异常';
                    if ($value['end_work'] < $pm_early) {
                        //下午早退;
                        $value['pm_status'] = Const_LeaveType::KQ_PM_STATUS_2;
                        $value['leave_early'] = 1;
                    }elseif ($value['end_work'] < $pm_absentee){
                        //下午缺勤
                        $value['pm_status'] = Const_LeaveType::KQ_PM_STATUS_4;
                        $value['days'] = 0.5;
                    }
                }else{//没有请假，有公出，则判断公出是否覆盖当前时间点，如果没有覆盖，则考勤依旧异常，否则修正。
                    $gc_time = explode('-', $leave['leave_gc_time']);
                    $travel_to_kq_status = $bll_daily_report->travel_to_kq_status($value['user_id'], $last_day, $gc_time['0'], $gc_time['1']);
                    if ($travel_to_kq_status['0'] = 5 && $travel_to_kq_status['1'] = 6) {
                    }
                }
            }

            $sql = "UPDATE `user_day_status` SET ".
                 " `days` 				=  ".$value['days'].
                 " `pm_status` 			=  ".$value['pm_status'].
                 "  WHERE `user_id` 	=  ".$value['user_id'];

            $this->execute($sql,array(),$this->writedb);
        }
    }

    public function update_status(){
        $user_status = $this->init_user_access_record();
        foreach ($user_status as $user_status){

            if (empty($user_status['start_work']) && empty($user_status['end_work'])) {
            //没有打卡记录
                //判断初始请假状态
                if (empty($user_status['am_type'])) {
                    //上午缺勤
                    $user_status['am_status'] = Const_LeaveType::KQ_AM_STATUS_3;
                    $days = 0.5;
                }elseif (empty($user_status['pm_type'])){
                    //下午缺勤
                    $user_status['pm_status'] = Const_LeaveType::KQ_PM_STATUS_4;
                    $days = 0.5;
                }
            }else{

            }
        }
    }

    /**
     * 遍历 user_day_status 通过 user_id 查询 access_day_record,得到初始状态打卡记录
     */
    public function init_user_access_record(){
        $result = array();
        //获取上午初始打开记录
        $all_user_day_status = $this->_get_all_user_day_status();
        $all_user_access_day_record = $this->_get_all_access_day_record();

        foreach ($all_user_day_status as $status){
            foreach ($all_user_access_day_record as $access){

                if ($status['user_id'] == $access['user_id']) {
                    //当天有打开记录
                    $all_user_day_status['start_work']    = $access['start_work'];
                    $all_user_day_status['end_work']      = $access['end_work'];
                }else {
                    //当天没有打开记录
                    $all_user_day_status['start_work']    = 0;
                    $all_user_day_status['end_work']      = 0;
                }
            }
        }

        return $all_user_day_status;

    }


    /**
     * 获取所有用户初始打卡记录
     * @return multitype:
     */
    public function _get_all_user_day_status(){
        $sql = "SELECT user_id, am_type ,am_leave_status, pm_type, pm_leave_status FROM user_day_status WHERE dateline=".$this->date;
        return $this->get_db_rows($sql,$this->writedb,array());
    }

    /**
     * 获取 0 点前一天所有用户打卡记录
     * @return multitype:
     */
    public function _get_all_access_day_record(){
        $sql = "SELECT user_id, start_work, end_work, accesss_date FROM access_day_record WHERE access_daste=".$this->date;
        return $this->get_db_rows($sql,$this->writedb,array());
    }

    /**
     * 获取当天每个员工的打卡记录，如果该员工没有打卡记录，则设置 start_work=0,end_work=0
     * @param array $uids
     */
    public function get_user_day_record(){

        $bll = new Bll_Attendance_AccessDayRecord();
        $last_day = date('Ymd',time()-24*60*60);//上一天的时间点
        $bll = new Bll_Attendance_UserKqStatus();
        $user_day_status = $bll->get_user_day_status();

        $day_status = array();

        foreach ($user_day_status as $key => $value){
            $result = $bll->get_record_list($value['user_id'],$last_day,$last_day);//获取员工上一天打卡记录
            if (!empty($result)) {
                $day_status[$key]['start_work'] = $result['start_work'];
                $day_status[$key]['end_work'] = $result['end_work'];
            }else {
                $day_status[$key]['start_work'] = 0;
                $day_status[$key]['end_work'] = 0;
            }
        }

        return $day_status;
    }


}